home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / checkbox / scripts / internet_test < prev    next >
Encoding:
Text File  |  2009-04-27  |  3.7 KB  |  131 lines

  1. #!/usr/bin/python
  2.  
  3. import os
  4. import re
  5. import sys
  6.  
  7. import commands
  8. import logging
  9. import socket
  10. import struct
  11.  
  12. from optparse import OptionParser
  13.  
  14.  
  15. class Route(object):
  16.     """Gets routing information from the system.
  17.     """
  18.  
  19.     # auxiliary functions
  20.     def _hex_to_dec(self, string):
  21.         """Returns the integer value of a hexadecimal string s
  22.         """
  23.         return int(string, 16)
  24.  
  25.     def _num_to_dotted_quad(self, number):
  26.         """Convert long int to dotted quad string
  27.         """
  28.         return socket.inet_ntoa(struct.pack("<L", number))
  29.  
  30.     def _get_default_gateway_from_proc(self):
  31.         """"Returns the current default gateway, reading that from /proc
  32.         """
  33.         logging.debug("Reading default gateway information from /proc")
  34.         try:
  35.             file = open("/proc/net/route")
  36.             route = file.read()
  37.         except:
  38.             logging.error("Failed to read def gateway from /proc")
  39.             return None
  40.         else:
  41.             h = re.compile("\n(?P<interface>\w+)\s+00000000\s+(?P<def_gateway>[\w]+)\s+")
  42.             w = h.search(route)
  43.             if w:
  44.                 if w.group("def_gateway"):
  45.                     return self._num_to_dotted_quad(self._hex_to_dec(w.group("def_gateway")))
  46.                 else:
  47.                     logging.error("Could not find def gateway info in /proc")
  48.                     return None
  49.             else:
  50.                 logging.error("Could not find def gateway info in /proc")
  51.                 return None
  52.  
  53.     def _get_default_gateway_from_bin_route(self):
  54.         """Get default gateway from /sbin/route -n
  55.         Called by get_default_gateway and is only used if could not get that from /proc
  56.         """
  57.         logging.debug("Reading default gateway information from route binary")
  58.         routebin = commands.getstatusoutput("export LANGUAGE=C; /usr/bin/env route -n")
  59.  
  60.         if routebin[0] == 0:
  61.             h = re.compile("\n0.0.0.0\s+(?P<def_gateway>[\w.]+)\s+")
  62.             w = h.search(routebin[1])
  63.             if w:
  64.                 def_gateway = w.group("def_gateway")
  65.                 if def_gateway:
  66.                     return def_gateway
  67.  
  68.         logging.error("Could not find default gateway by running route")
  69.         return None
  70.  
  71.     def get_hostname(self):
  72.         return socket.gethostname()
  73.  
  74.     def get_default_gateway(self):
  75.         t1 = self._get_default_gateway_from_proc()
  76.         if not t1:
  77.             t1 = self._get_default_gateway_from_bin_route()
  78.  
  79.         return t1
  80.  
  81. def ping(host, count, deadline):
  82.     command = "ping -q -c %s -w %s %s" % (count, deadline, host)
  83.     reg = re.compile(r"(\d) received")
  84.     packets_received = 0
  85.  
  86.     output = os.popen(command)
  87.     for line in output.readlines():
  88.         received = re.findall(reg, line)
  89.         if received:
  90.             packets_received = int(received[0])
  91.  
  92.     return packets_received
  93.  
  94. def main(args):
  95.     default_count = 2
  96.     default_delay = 4
  97.  
  98.     usage = "%prog [HOST]"
  99.     parser = OptionParser(usage=usage)
  100.     parser.add_option("-c", "--count",
  101.         default=default_count,
  102.         type="int",
  103.         help="Number of packets to send.")
  104.     parser.add_option("-d", "--deadline",
  105.         default=default_delay,
  106.         type="int",
  107.         help="Timeouts in seconds.")
  108.     (options, args) = parser.parse_args(args)
  109.  
  110.     if args:
  111.         host = args.pop(0)
  112.     else:
  113.         route = Route()
  114.         host = route.get_default_gateway()
  115.  
  116.     received_packets = 0
  117.     if host:
  118.         received_packets = ping(host, options.count, options.deadline)
  119.  
  120.     if received_packets == 0:
  121.         print "No Internet connection"
  122.     elif received_packets != options.count:
  123.         print "Connection established lost a packet"
  124.     else:
  125.         print "Internet connection fully established"
  126.  
  127.     return 0
  128.  
  129. if __name__ == "__main__":
  130.     sys.exit(main(sys.argv[1:]))
  131.